﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.PagerControl;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.IO;
using System.Resources;
using System.Web.Security;
public partial class admin_UserList : SitePage
{
    #region Init,Loads
    private int maxLevel = 0;
    busUserProfile _bus = new busUserProfile();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            clsBindControl.BindBooleanCombobox(F_ddlIsAvtive);
            LoadData();
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
    #endregion

    #region Events
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserList.aspx");
    }
    protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            UserProfile objData = (UserProfile)e.Item.DataItem;
            if (objData == null)
                return;
            busaspnet_Roles objBusRole = new busaspnet_Roles();
            int level = objBusRole.GetMaxRoleLevel(this.ApplicationName, objData.UserId);
            if (level > maxLevel)
            {
                e.Item.Visible = false;
                return;
            }
            MembershipUser user = Membership.GetUser(objData.UserId);
            hidID.Value += objData.UserName.ToString() + ";";
            Literal ltr = (Literal)e.Item.FindControl("ltrFirstName");
            ltr.Text = objData.FirstName;
            ltr = (Literal)e.Item.FindControl("ltrLastName");
            ltr.Text = objData.LastName;
            //image button IsActive
            ImageButton imgButton = (ImageButton)e.Item.FindControl("imbActive");
            imgButton.CommandArgument = objData.UserName.ToString();
            if (user.IsApproved == true)
            {
                imgButton.ImageUrl = clsString.ACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_DEACTIVE;
                imgButton.CommandName = "DeApproved";
            }
            else
            {
                imgButton.ImageUrl = clsString.DEACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_ACTIVE;
                imgButton.CommandName = "Approved";
            }

            imgButton = (ImageButton)e.Item.FindControl("imbDelete");
            imgButton.ToolTip = Resources.Resource.TIP_DELETE;
            imgButton.Attributes.Add("onclick", "return confirm('" + Resources.Resource.MSG_CONFIRM_DELETE + "');");
            imgButton.CommandArgument = objData.UserName.ToString();
            imgButton.ImageUrl = clsString.DELETE_IMAGE_URL;
        }
    }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string userName = e.CommandArgument.ToString();
        switch (e.CommandName)
        {
            case "Approved":
                {
                    Active(userName);
                    break;
                }
            case "DeApproved":
                {
                    DeActive(userName);
                    break;
                }
            case "Delete":
                {
                    Delete(userName);
                    LoadData();
                    break;
                }
        }

    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserDetail.aspx");
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string[] arr = hidIDSelect.Value.Split(';');
        for (int i = 0; i < arr.Length - 1; i++)
        {
            if (arr[i].ToString() != string.Empty)
            {

                Delete(arr[i].ToString());
            }
        }
        LoadData();
    }
    #endregion

    #region Methods
    private void Active(string userName)
    {
        MembershipUser user = Membership.GetUser(userName);
        if (user.ProviderUserKey.ToString() == this.userloginId.ToString())
            return;
        if (user != null)
        {
            user.IsApproved = true;
            Membership.UpdateUser(user);
            LoadData();
        }
    }
    private void DeActive(string userName)
    {
        MembershipUser user = Membership.GetUser(userName);
        if (user.ProviderUserKey.ToString() == this.userloginId.ToString())
            return;
        if (user != null)
        {
            user.IsApproved = false;
            Membership.UpdateUser(user);
            LoadData();
        }
    }
    private void Delete(string username)
    {
        MembershipUser user = Membership.GetUser(username);
        if (user.ProviderUserKey.ToString() == this.userloginId.ToString())
            return;
        Membership.DeleteUser(username, true);
        _bus.Delete(new Guid(user.ProviderUserKey.ToString()));
    }
    public int count = 0;
    private void LoadData()
    {
        busaspnet_Roles objBusRole = new busaspnet_Roles();
        maxLevel = objBusRole.GetMaxRoleLevel(this.ApplicationName, this.userloginId);
        hidID.Value = string.Empty;
        ///  paraURL = GetPageParams(FilterFieldType.All);
        hidID.Value = string.Empty;
        //get Condition
        UserProfile objProfile = new UserProfile();
        objProfile.CurrentPage = 1;
        objProfile.OrderBy = "UserName";
        objProfile.OrderDirection = "ASC";
        objProfile.PageSize = int.MaxValue;
        objProfile.Condition = " 1=1 ";
        if (!string.IsNullOrEmpty(F_txtUserCode.Text))
        {
            objProfile.Condition+= " and p.UserCode='" + F_txtUserCode.Text.Trim() + "'";
        }
        if (!string.IsNullOrEmpty(F_txtUserName.Text))
        {
            objProfile.Condition+= " and u.UserName='" + F_txtUserName.Text.Trim() + "'";
        }

        if (F_ddlIsAvtive.SelectedValue != string.Empty)
            objProfile.Condition += " and ms.IsApproved=" + F_ddlIsAvtive.SelectedValue.ToString();
        int total = 0;
        UserProfileCollection lstResult = _bus.List(objProfile);
        total = objProfile.TotalRecords;
        if (lstResult != null && lstResult.Count > 0)
        {
            count = lstResult.Count;
            rptData.DataSource = lstResult;
            divGrid.Visible = true;
            divEmplty.Visible = false;
        }
        else
        {
            rptData.DataSource = null;
            divGrid.Visible = false;
            divEmplty.Visible = true;
            divEmplty.InnerHtml = Resources.Resource.MSG_EMPTY;
        }

        rptData.DataSource = lstResult;
        rptData.DataBind();


    }
    #endregion

}